<?php
///////
// IdleIRC - 2020
// (C) Chris Dorman, GPLv3
// https://notabug.org/Pentium44/idleirc
///////

// irc.php - used to push and pull data from IRC server.
// Currently supports PING / PONG, data receive, and data push
// Done via PHP sockets.

// Prevent PHP from stopping the script after 30 sec
set_time_limit(0);

// Include variables
include_once("config.php");

// Get username from command line argument / PHP-CLI
$username = $argv[1];
$servaddr = $argv[2]; // If server address is specified
$servport = $argv[3]; // If server port is specified

if(file_exists("users/$username.php")) {
	include("users/$username.php");
}

$server_address = isset($servaddr) ? $servaddr : $server;
$server_port = isset($servport) ? $servport : $port;

function usernameInMsg($username, $msgString) {
    if(strpos($msgString, $username) !== false){
        return true;
    } else{
        return false;
    }
}

// Function to search for username
function get_string_between($string, $start, $end){
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}

// If username isn't set, exit with error.
if(!isset($username) || $username == "") {
	echo "Username not given...";
	exit(1);
}

// Create a socket to use
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

// Send first packets to client to inform connection
$socketFileContents = file_get_contents("users/$username.log"); // get current log contents
$msgline = "<tr><td class='userinfo'><span style='color:$ipcolor;'>" . $server_address . "</span> ~ </td><td>Connecting to $server_address:$server_port... please wait a few</td></tr>\n";
file_put_contents("users/$username.log", $socketFileContents . $msgline); 

// Connect to IRC server via socket above
socket_connect($socket, $server_address, $server_port);

sleep(2);

// NICK and USER calls to IRC 
$nickline = "NICK " . $usernickname . "\n";
$userline = "USER " . $usernickname . " 0 * :" . $username . "'s $title $version client\n";

// Pass NICK and USER back to IRC server over socket
socket_write($socket, $nickline, strlen($nickline));
// Sleep for a second before sending USER lineage
sleep(1);
// SEND IT
socket_write($socket, $userline, strlen($userline));

//sleep(1);

// Continue the rest of the script here
// While script will continue as long as socket continues to be active
while($bytes = socket_recv($socket, $r_data, 2048, MSG_DONTWAIT) !== '') {
	if($bytes !== FALSE) {
		//$data = socket_read($socket, 2048, PHP_NORMAL_READ);
		$packetdata = $r_data;
	}

	// If client sent something, push it to the IRC server!
	if(file_exists("users/.$username.push")) {
		// Grab IRC command from server.php
		$pushFile = file_get_contents("users/.$username.push");
		// Push this / these commands to socket
		socket_write($socket, $pushFile, strlen($pushFile));
		// Remove the push file
		unlink("users/.$username.push");
	}
        
	// Check if web client still up, if no pong after 15 seconds, connection closed.
	if(!file_exists("users/.$username.pingfile")) { // If file is missing, quit
		// Debug logging, check if IRC is exiting properly
		doLog("Exiting, $username logged out...");
		$quitline = "QUIT :$username toggled disconnect; $title $version\n"; // IRC QUIT line
		socket_write($socket, $quitline, strlen($quitline)); // Push to socket
		socket_close($socket); // Close the socket
		exit(0); // Exit the script, nothing to do.
	}
	
	// If data variable is set and buffer has data to recieve
	// RECIEVE IT!
	if(isset($packetdata)) { // If data variable is set, there's data from socket
        //doLog("Server received chunk: $packetdata");

        $lines = explode("\r\n", $packetdata);

        foreach($lines as $data) {
            if($data == "") { continue; }

            //doLog("Line: $data");

            $socketFileContents = file_get_contents("users/$username.log");
    		$stringMsg = explode('PRIVMSG', $data); // Strip IRC commands
    		$ex = explode(' ', $data);
    		$data = htmlentities($data);

            if($ex[1] == "376" || $ex[1] == "366") { continue; }

	    	// Send PONG back to the server
	    	if ($ex[0] == "PING") {
	    		// Log pong
	    		doLog("PONG, $username response...");
	    		$pongline = "PONG " . $ex[1] . "\n"; // PONG IRC CMD
	    		// Push to IRC server via socket.
	    		socket_write($socket, $pongline, strlen($pongline));
	    	} else if ($ex[1] == "353") {
                // Userlist has been captured, lets generate it for the client.
                // Grabs raw user list
                $userlistContent = "<b>" . $ex[4] . "</b><br />";
                $userlistRaw = explode($ex[4] . " :", $data);
                $userArray = explode(" ", $userlistRaw[1]);
                // Generate a list of users, in HTML format for userlist
                foreach($userArray as $usi) {
                    $userlistContent .= "<a onclick='UserPM(\"$usi\");' href='#'>$usi</a> <br />";
                }

                file_put_contents("users/$username.userlist", $userlistContent);
            } else if ($ex[1] == "PART") {
	    		$senderNick = get_string_between($data, ":", "!");
	    		$senderIp = get_string_between($data, "@", " ");
	     		$exitMsg = explode('PART', $data);
                $partChannel = explode(' :', $exitMsg[1]);
		    	$msgline = "<tr><td class='userinfo'><b>$senderNick</b>: Leaving<br /><span style='color:$ipcolor;font-size:9px;'>$senderIp</span></td><td> left " . stripslashes(trim($exitMsg[1])) . "</td></tr>";
                file_put_contents("users/$username.log", $socketFileContents . $msgline);
                file_put_contents("users/.$username.push", "NAMES " . $partChannel[0] . "\n");
	    	} else if ($ex[1] == "JOIN") {
                $senderNick = get_string_between($data, ":", "!");
		    	$senderIp = get_string_between($data, "@", " ");
                $joinChannel = explode("JOIN :", $data);
			    if($senderNick != $usernickname) {
	                $msgline = "<tr><td class='userinfo'><b>$senderNick</b>:" . $joinChannel[1] . "<br /><span style='color:$ipcolor;font-size:9px;'>$senderIp</span></td><td> joined " . $joinChannel[1] . "</td></tr>\n";
	                file_put_contents("users/$username.log", $socketFileContents . $msgline);
			    } else {
		    		$msgline = "<tr><td class='userinfo'><span style='color:$ipcolor;'>" . $server_address . "</span> ~ </td><td>Joining " . $joinChannel[1] . "</td></tr>\n";
		    		file_put_contents("users/$username.log", $socketFileContents . $msgline);
    			}
                file_put_contents("users/.$username.push", "NAMES " . $joinChannel[1] . "\n");
    		} else if ($ex[1] == "NICK") {
    			$senderNick = get_string_between($data, ":", "!");
                $senderIp = get_string_between($data, "@", " ");
    			$nickMsg = explode('NICK :', $data);
                $msgline = "<tr><td class='userinfo'><b>$senderNick</b>:" . trim($nickMsg[1]); 
                $msgline .= "<br /><span style='color:$ipcolor;font-size:9px;'>$senderIp</span>";
                $msgline .= "</td><td> $senderNick is now known as" . trim($nickMsg[1]);
                $msgline .= "</td></tr>\n";
    			file_put_contents("users/$username.log", $socketFileContents . $msgline);
            } else if ($ex[1] == "QUIT") {
                $senderNick = get_string_between($data, ":", "!");
                $senderIp = get_string_between($data, "@", " ");
                $quitMsg = explode('QUIT :', $data);
                $msgline = "<tr><td class='userinfo'><b>$senderNick</b><br /><span style='color:$ipcolor;font-size:9px;'>$senderIp</span></td><td> $senderNick left: " . trim($quitMsg[1]) . "</td></tr>\n";
                file_put_contents("users/$username.log", $socketFileContents . $msgline);
                file_put_contents("users/.$username.push", "NAMES");
            } else if ($ex[2] == $usernickname && $ex[1] == "PRIVMSG") {
			    $senderNick = get_string_between($data, ":", "!");
		    	$senderIp = get_string_between($data, "@", " ");
		    	$privMsg = explode($usernickname . " :", $stringMsg[1]);
                file_put_contents("users/.$username.pmed", "$senderNick");
			    $msgline = "<tr><td class='userinfo'>PM from <b>$senderNick</b><br /><span style='color:$ipcolor;font-size:9px;'>$senderIp</span></td><td> " . htmlentities(stripslashes(trim($privMsg[1]))) . "</td></tr>\n";
                file_put_contents("users/$username.log", $socketFileContents . $msgline);
			    $msg = "";
	    	} else if ($stringMsg[1] != "") {
    			$senderNick = get_string_between($data, ":", "!");
	    		$senderIp = get_string_between($data, "@", " ");
	    		$channel = explode(" :", $stringMsg[1]);
	    		$msg = explode($channel[0] . " :", $stringMsg[1]);

	    		$msgline = "<tr><td class='userinfo'><b>$senderNick</b>:" . $channel[0] . "<br /><span style='color:$ipcolor;font-size:9px;'>$senderIp</span></td><td> " . htmlentities(stripslashes(trim($msg[1]))) . "</td></tr>";
                if(usernameInMsg($usernickname, $msg[1])==true) { 
                    file_put_contents("users/.$username.mentioned", "$senderNick");
                }

                file_put_contents("users/$username.log", $socketFileContents . $msgline);
	    	} else if ($ex[0] == ":$server_address") {
		    	$msgline = "<tr><td class='userinfo'><span style='color:$ipcolor;'>" . $server_address . "</span> ~ </td><td> " . $data . "</td></tr>\n";
		    	file_put_contents("users/$username.log", $socketFileContents . $msgline);
		    }
        }
	}
	
	// second sleep to prevent insane CPU load	
	usleep(250000);
}

?>
